// 检查数据库集合是否存在的云函数
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  try {
    const collections = [
      'users',
      'activities', 
      'activity_registrations',
      'activity_members',
      'user_activity_relay'
    ]
    
    const results = {}
    
    for (const collectionName of collections) {
      try {
        // 尝试获取集合信息
        const result = await db.collection(collectionName).limit(1).get()
        results[collectionName] = {
          exists: true,
          count: result.data.length,
          message: '集合存在'
        }
      } catch (error) {
        results[collectionName] = {
          exists: false,
          error: error.message,
          message: '集合不存在或无权限访问'
        }
      }
    }
    
    return {
      success: true,
      data: results
    }
    
  } catch (error) {
    console.error('检查数据库失败:', error)
    return {
      success: false,
      message: error.message
    }
  }
}